<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="健康人" prop="healthId">
        <el-input v-model="queryParams.healthId" placeholder="健康信息ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="设备" prop="deviceId">
        <el-input v-model="queryParams.deviceId" placeholder="设备ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="方案" prop="schemeId">
        <el-input v-model="queryParams.schemeId" placeholder="方案ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="待检测" :value="0" />
          <el-option label="检测中" :value="1" />
          <el-option label="已完成" :value="2" />
          <el-option label="失败" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['geek:detectTask:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['geek:detectTask:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['geek:detectTask:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['geek:detectTask:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务ID" align="center" prop="taskId" />
      <el-table-column label="健康人" align="center" prop="userName" />
      <el-table-column label="设备" align="center" prop="deviceName" />
      <el-table-column label="方案" align="center" prop="schemeName" />
      <el-table-column label="机构" align="center" prop="deptName" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-tag v-if="scope.row.status === 0">待检测</el-tag>
          <el-tag type="info" v-else-if="scope.row.status === 1">检测中</el-tag>
          <el-tag type="success" v-else-if="scope.row.status === 2">已完成</el-tag>
          <el-tag type="danger" v-else-if="scope.row.status === 3">失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="检测结果" align="center" prop="resultUrl">
        <template #default="scope">
          <el-link v-if="scope.row.resultUrl" :href="scope.row.resultUrl" target="_blank">查看结果</el-link>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="finishTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.finishTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="text" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['geek:detectTask:edit']">修改</el-button>
          <el-button type="text" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['geek:detectTask:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="健康信息ID" prop="healthId">
          <el-input v-model="form.healthId" placeholder="请输入健康信息ID" type="number" />
        </el-form-item>
        <el-form-item label="设备ID" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备ID" type="number" />
        </el-form-item>
        <el-form-item label="方案ID" prop="schemeId">
          <el-input v-model="form.schemeId" placeholder="请输入方案ID" type="number" />
        </el-form-item>
        <el-form-item label="外部系统ID" prop="externalId">
          <el-input v-model="form.externalId" placeholder="请输入外部系统ID" maxlength="50" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="待检测" :value="0" />
            <el-option label="检测中" :value="1" />
            <el-option label="已完成" :value="2" />
            <el-option label="失败" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="检测结果URL" prop="resultUrl">
          <el-input v-model="form.resultUrl" placeholder="请输入检测结果URL" maxlength="255" />
        </el-form-item>
        <el-form-item label="完成时间" prop="finishTime">
          <el-date-picker v-model="form.finishTime" type="datetime" placeholder="请选择完成时间" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" maxlength="255" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="DetectTask">
import { listDetectTask, getDetectTask, addDetectTask, updateDetectTask, delDetectTask, exportDetectTask } from '@/api/geek/detectTask'
import { getCurrentInstance } from 'vue'

const { proxy } = getCurrentInstance()

const list = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    healthId: undefined,
    deviceId: undefined,
    schemeId: undefined,
    status: undefined
  },
  rules: {
    healthId: [{ required: true, message: '健康信息ID不能为空', trigger: 'blur' }],
    deviceId: [{ required: true, message: '设备ID不能为空', trigger: 'blur' }],
    schemeId: [{ required: true, message: '方案ID不能为空', trigger: 'blur' }]
  }
})
const { queryParams, form, rules } = toRefs(data)

function getList() {
  loading.value = true
  listDetectTask(queryParams.value).then(res => {
    list.value = res.rows
    total.value = res.total
    loading.value = false
  })
}

function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.taskId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

function handleAdd() {
  reset()
  open.value = true
  title.value = '添加检测任务'
}

function handleUpdate(row) {
  reset()
  const id = row?.taskId || ids.value[0]
  getDetectTask(id).then(res => {
    form.value = res.data
    open.value = true
    title.value = '修改检测任务'
  })
}

function submitForm() {
  proxy.$refs['formRef'].validate(valid => {
    if (valid) {
      if (form.value.taskId != undefined) {
        updateDetectTask(form.value).then(() => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addDetectTask(form.value).then(() => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

function handleDelete(row) {
  const delIds = row?.taskId ? [row.taskId] : ids.value
  if (!delIds.length) return
  proxy.$modal.confirm('是否确认删除选中的检测任务？', '提示').then(() => {
    delDetectTask(delIds.join(',')).then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
  }).catch(() => {})
}

function handleExport() {
  exportDetectTask(queryParams.value).then(res => {
    proxy.download(res, '检测任务数据.xlsx')
  })
}

function reset() {
  form.value = {
    taskId: undefined,
    healthId: undefined,
    deviceId: undefined,
    schemeId: undefined,
    externalId: undefined,
    status: 0,
    resultUrl: undefined,
    finishTime: undefined,
    remark: undefined
  }
  proxy.resetForm('formRef')
}

function cancel() {
  open.value = false
  reset()
}

getList()
</script> 